<template>
  <div>
    <!-- echarts -->
    <div class="myChart" ref="chartRef"></div>
  </div>
</template>

<script>
import * as echarts from 'echarts'
export default {
  // 父传子
  props: {
    chartData: Object,
  },
  methods: {
    //画折线图
    renderLine(obj) {
      //默认值
      let defaultOptions = {
        title: '默认标题',
      }
      //最终参数 传入参数合并默认值
      let finalObj = {...defaultOptions, ...obj}

      // 基于准备好的dom，初始化echarts实例
      let myChart = echarts.init(this.$refs.chartRef)
      //配置项
      let options = {
        title: {
          text: finalObj.title,
          left: 15,
          top: 15,
        },
        tooltip: {
          trigger: 'axis',
        },
        legend: {
          top: 15,
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true,
        },
        toolbox: {
          feature: {
            saveAsImage: {},
          },
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: finalObj.xData,
        },
        yAxis: {
          type: 'value',
        },
        //核心数据
        series: finalObj.series,
      }
      // 绘制图表
      myChart.setOption(options)
    },
  },
  // 方法二
  watch: {
    chartData() {
      this.renderLine(this.chartData)
    },
  },

  // 方法一
  // mounted() {
  //   this.renderLine(this.chartData)
  // },
}
</script>

<style lang="scss" scoped>
.page-order {
  height: 100%;
  background: #f0f2f5;
}

.myChart {
  margin-top: 50px;
  height: 300px;
  background: white;
}
</style>
